package cn.edu.nju.software.greencreditsystem.DAOImpl;

import cn.edu.nju.software.greencreditsystem.DAO.PredictionDAO;
import cn.edu.nju.software.greencreditsystem.domain.Prediction;
import cn.edu.nju.software.greencreditsystem.factory.SessionPool;
import java.util.List;
import org.hibernate.Query;
import org.hibernate.Session;

/**
 * 作者：何启睿
 * 创建时间：2011-8-8
 * 类描述：预警类DAO实现
 */
public class PredictionDAOImpl implements PredictionDAO
{

    @Override
    public boolean save(Prediction p)
    {
        boolean result = false;
        
        Session s = SessionPool.getSession();
        s.beginTransaction();
        try
        {
            s.save(p);
            s.getTransaction().commit();
            result = true;
        }catch(Exception ex)
        {
            ex.printStackTrace();
            s.getTransaction().rollback();
        }
        finally
        {
            s.close();
        }
        
        return result;
    }

    @Override
    public boolean delete(Prediction p)
    {
        boolean result = false;
        
        Session s = SessionPool.getSession();
        s.beginTransaction();
        try
        {
            s.delete(p);
            s.getTransaction().commit();
            result = true;
        }catch(Exception ex)
        {
            ex.printStackTrace();
            s.getTransaction().rollback();
        }
        finally
        {
            s.close();
        }
        
        return result;
    }

    @Override
    public boolean update(Prediction p)
    {
        boolean result = false;
        
        Session s = SessionPool.getSession();
        s.beginTransaction();
        try
        {
            s.update(p);
            s.getTransaction().commit();
            result = true;
        }catch(Exception ex)
        {
            ex.printStackTrace();
            s.getTransaction().rollback();
        }
        finally
        {
            s.close();
        }
        
        return result;
    }

    @Override
    public Prediction getById(int id)
    {
        Prediction result = null;
        
        Session s = SessionPool.getSession();
        s.beginTransaction();
        try
        {
            result = (Prediction) s.get(Prediction.class, id);
            s.getTransaction().commit();
        }catch(Exception ex)
        {
            ex.printStackTrace();
            s.getTransaction().rollback();
        }
        finally
        {
            s.close();
        }
        
        return result;
    }

    @Override
    public List<Prediction> getAll(int enterpriseID)
    {
        List<Prediction> result = null;
        
        Session s = SessionPool.getSession();
        s.beginTransaction();
        try
        {
            Query q = s.createQuery("from Prediction where enterprise.id = " + enterpriseID + " order by date desc");
            result = q.list();
            s.getTransaction().commit();
        }catch(Exception ex)
        {
            ex.printStackTrace();
            s.getTransaction().rollback();
        }
        finally
        {
            s.close();
        }
        
        return result;
    }

    @Override
    public List<Prediction> getUnread(int enterpriseID)
    {
        List<Prediction> result = null;
        
        Session s = SessionPool.getSession();
        s.beginTransaction();
        try
        {
            Query q = s.createQuery("from Prediction where enterprise.id = " + enterpriseID
                                     + " and readed=false order by date desc");
            result = q.list();
            s.getTransaction().commit();
        }catch(Exception ex)
        {
            ex.printStackTrace();
            s.getTransaction().rollback();
        }
        finally
        {
            s.close();
        }
        
        return result;
    }

    @Override
    public List<Prediction> getLatest(int enterpriseID, int num)
    {
        List<Prediction> result = null;
        
        Session s = SessionPool.getSession();
        s.beginTransaction();
        try
        {
            Query q = s.createQuery("from Prediction "
                                    + "where enterprise.id=" + enterpriseID
                                    + " order by date desc");
            result = q.list();
            s.getTransaction().commit();
        }catch(Exception ex)
        {
            ex.printStackTrace();
            s.getTransaction().rollback();
        }
        finally
        {
            s.close();
        }
        
        if(result.size() > num)
            return result.subList(0, num);
        else
            return result;
    }

    @Override
    public Prediction getLatest(int enterpriseID)
    {
        List<Prediction> list = getLatest(enterpriseID, 1);
        if(list.size() > 0)
            return list.get(0);
        else
            return null;
    }

    @Override
    public List<Prediction> getUnread()
    {
        List<Prediction> result = null;
        
        Session s = SessionPool.getSession();
        s.beginTransaction();
        try
        {
            Query q = s.createQuery("from Prediction where readed=false order by date desc");
            result = q.list();
            s.getTransaction().commit();
        }catch(Exception ex)
        {
            ex.printStackTrace();
            s.getTransaction().rollback();
        }
        finally
        {
            s.close();
        }
        
        return result;
    }
    
}
